<script setup lang="ts">
import { getCodeAPI, postLoginAPI } from '@/apis/common'
import type { PostLoginParams } from '@/types/common'
import { useUserStore } from '@/stores/user'
import { ref } from 'vue'

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})
// 是否显示倒计时
const showCountDown = ref(false)
// 获取验证码
const handleGetCode = async () => {
  await formRef.value?.validateField?.(['mobile'])
  showCountDown.value = true
  // 调用接口，通知后端发送验证码到用户手机
  const res = await getCodeAPI({ mobile: formDara.value.mobile, type: 'login' })
  console.log('测试阶段返回值有验证码', res)
}
// 倒计时结束
const handleTimeup = () => {
  console.log('获取验证码')
  showCountDown.value = false
}

// 1. 获取表单数据
const formDara = ref<PostLoginParams>({
  mobile: '13230000082',
  code: '',
})
// 2.表单校验
const formRules = ref<UniHelper.UniFormsRules>({
  mobile: {
    rules: [
      { required: true, errorMessage: '请输入手机号' },
      { pattern: /^1[3-9]\d{9}$/, errorMessage: '请输入正确的手机号' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '请输入正确的验证码' },
    ],
  },
})
// 3. 点击登录校验整个表格
const formRef = ref<UniHelper.UniFormsInstance>()
const isAgree = ref(false)
//4. 登录
const handleLogin = async () => {
  if (!isAgree.value) {
    return uni.showToast({ title: '请同意用户协议和隐私协议', icon: 'none' })
  }
  formRef.value?.validate?.()
  // 发送表单给后端，获取登录结果
  const res = await postLoginAPI(formDara.value)
  // console.log('登录结果', res)
  // 获取token
  const store = useUserStore()
  store.setToken(res.data.token)
  store.setUserInfo(res.data)
  // 请提示
  uni.showToast({ title: '登录成功', icon: 'success' })
  // 跳转至首页
  uni.switchTab({ url: '/pages/index/index' })
}
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :model="formDara" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :maxlength="11"
        v-model="formDara.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :maxlength="6"
        v-model="formDara.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text class="text-button" v-if="!showCountDown" @tap="handleGetCode">获取验证码</text>
      <view class="text-button" v-else>
        <!-- 倒计时组件 -->
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :day="0"
          :hour="0"
          :minute="0"
          :second="59"
          @timeup="handleTimeup"
        />
        秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @tap="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @tap="handleLogin">登 录</button>
  </uni-forms>
</template>

<style lang="scss">
@import './styles.scss';
</style>
